﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace guest_management_in_a_hotel
{
    public partial class FrmDangky : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlCommand cmd1;
        SqlCommand cmd2;
        SqlDataReader rd;
        SqlDataReader rd1;
        SqlDataReader rd2;
        public FrmDangky()
        {
            InitializeComponent();
        }

        private void FrmDangky_Load(object sender, EventArgs e)
        {
            lbsophong.Text = FrmQuanLyPhong.sophong1;
            lbkieuphong.Text = FrmQuanLyPhong.kieuphong;
            lbgia.Text = FrmQuanLyPhong.gia;
            lbghichu.Text = FrmQuanLyPhong.ghichu;
        }

        private void btnFrmdkOK_Click(object sender, EventArgs e)
        {
            if (tbhoten.Text == "" || cmbgioitinh.Text == "" || tbngaysinh.Text == "" || tbngaythue.Text == "" || tbdiachi.Text == "" || tbquoctich.Text == "" || tbcmnd.Text == "" || tbngaytra.Text == "")
            {
                MessageBox.Show("Hãy kiểm tra lại thông tin!");
            }
            else
            {
                conn = Connect.getConnection();
                try
                {
                    if (conn != null)
                    {
                        conn.Open();
                        String strsql = "insert into TblGuests values('" + Int32.Parse(tbcmnd.Text) + "',N'" + tbhoten.Text + "',N'" + cmbgioitinh.Text + "','" + tbngaysinh.Text + "',N'" + tbdiachi.Text + "',N'" + tbquoctich.Text + "')";
                        String strsql1 = "insert into TblReserve values('" + Int32.Parse(lbsophong.Text) + "','" + Int32.Parse(tbcmnd.Text) + "','" + tbngaythue.Text + "','" + tbngayden.Text + "','" + tbngaytra.Text + "')";
                        String strsql2 = "UPDATE TblRooms SET status = N'đang sử dụng' WHERE Room_Number = '" + lbsophong.Text + "'";
                        cmd = new SqlCommand(strsql, conn);
                        rd = cmd.ExecuteReader();
                        conn.Close();
                        conn.Open();
                        cmd1 = new SqlCommand(strsql1, conn);
                        rd1 = cmd1.ExecuteReader();
                        conn.Close();
                        conn.Open();
                        cmd2 = new SqlCommand(strsql2, conn);
                        rd2 = cmd2.ExecuteReader();
                        conn.Close();
                        MessageBox.Show("Đăng ký phòng thành công");
                        FrmMain frmMain = new FrmMain();
                        frmMain.Show();
                        this.Hide();
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Lỗi đăng ký phòng", "Mời nhập lại");
                    
                }
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            tbcmnd.Clear();
            tbdiachi.Clear();
            tbhoten.Clear();
            tbquoctich.Clear();
        }

    }
}
